﻿Imports System.Data.OleDb

Public Class EditComments
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try


            If (Not Page.IsPostBack) Then


                If (Request.QueryString("EditCommentId") <> Nothing) Then
                    Dim myComment As CommentsManagement = New CommentsManagement()

                    Dim dr As OleDbDataReader = myComment.SelectCommentbyCommentID(Convert.ToInt32(Request.QueryString("EditCommentId")))


                    If dr.HasRows Then
                        dr.Read()
                        If Not IsDBNull(dr("CommentContent")) Then txtComment.Text = dr("CommentContent")

                    End If
                ElseIf (Request.QueryString("ApproveCommentId") <> Nothing) Then

                    Dim myComment As CommentsManagement = New CommentsManagement()
                    myComment.EditApproveCommentByCommentId(Convert.ToInt32(Request.QueryString("ApproveCommentId")))


                    Dim quizId As Int32 = -1

                    'get quizId

                    Dim myComment2 As CommentsManagement = New CommentsManagement()

                    Dim dr2 As OleDbDataReader = myComment2.SelectCommentbyCommentID(Convert.ToInt32(Request.QueryString("ApproveCommentId")))


                    If dr2.HasRows Then
                        dr2.Read()
                        If Not IsDBNull(dr2("QuizId")) Then quizId = Convert.ToInt32(dr2("QuizId"))

                    End If

                    'get email

                    Dim getEmail As QuizManagement = New QuizManagement()
                    Dim dr As OleDbDataReader = getEmail.SelectQuizTeacherEmailByQuizId(quizId)
                    Dim teacherEmail As String = ""

                    If dr.HasRows Then
                        dr.Read()
                        If Not IsDBNull(dr("Email")) Then teacherEmail = Convert.ToString(dr("Email"))

                    End If

                    Dim currentUrl As String = HttpContext.Current.Request.Url.AbsoluteUri
                    currentUrl = currentUrl.Replace("Admin/EditComments.aspx?ApproveCommentId=" + Request.QueryString("ApproveCommentId"), "QuizOverview.aspx?quizId=" + quizId.ToString())


                    Dim myEmail As EmailManagement = New EmailManagement(teacherEmail, "Visit the following link to read the new comment: " + currentUrl, "New Comment has been added")
                    myEmail.sendMail()

                    lblMessage.Text = "Comment Approved. Email has been sent to the respective teacher of the quiz that there is a new comment"

                ElseIf (Request.QueryString("DeleteCommentId") <> Nothing) Then

                    Dim myComment As CommentsManagement = New CommentsManagement()
                    myComment.DeleteCommentByCommentId(Convert.ToInt32(Request.QueryString("DeleteCommentId")))
                    lblMessage.Text = "Comment deleted"
                End If
            End If


            If (grvComments.Rows.Count <> 0) Then
                txtComment.Visible = True
                btnUpdate.Visible = True
            Else
                txtComment.Visible = False
                btnUpdate.Visible = False
                lblMessage.Text = "There are no comments"
            End If

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Try


            Dim myComment As CommentsManagement = New CommentsManagement()
            myComment.EditCommentByCommentId(Convert.ToInt32(Request.QueryString("EditCommentId")), txtComment.Text)
            grvComments.DataBind()
            lblMessage.Text = "Comment Updated"
        Catch ex As Exception

        End Try
    End Sub
End Class